package com.reflect.test;

import com.reflect.entity.Student;

import java.lang.reflect.Field;

/**
 * @author wzm
 * @create 2023-04-06 10:31
 */

//
public class RefTest05 {
    public static void main(String[] args) throws Exception {
     //获取类的字节码文件
       Class clazz = Class.forName("com.reflect.entity.Student");

       //创建一个student对象
        Student student = new Student(1,"张三",18);

        //获取name属性 private
        Field name = clazz.getDeclaredField("name");

        //修改临时权限
        name.setAccessible(true);

        //通过name属性 赋值 (该name属性赋值为哪个对象的值,值是什么)
        name.set(student,"王五");
        System.out.println("student = " + student);

        //通过反射获取name的值
//        student.getName()
        //获取的是哪个对象的name
        String s = (String) name.get(student);
        System.out.println("name = " + s);

    }

}
